<?php
namespace app\common\model\customer;
use app\common\model\user\User;
use app\api\controller\Pub;
use think\Model;

class CustomerProject extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'customer_project';
    protected $pk    = 'Id';

    public function typeinfo()
    {
        return $this->hasOne('app\common\model\customer\CustomerProjectType', 'Id', 'ProjectTypeId');
    }
    /**
     * 获取客户对应的项目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getlist($id)
    {
        $ndata = CustomerProject::where('CustomerId', $id)->with('typeinfo')->where('isDelete',0)->order('KeyTime desc,UpTime desc')->select();
        return $ndata;
    }

/**
 * 增加客户项目
 * @param  [type] $CustomerId    [description]
 * @param  [type] $ProjectTypeId [description]
 * @param  [type] $data          [description]
 * @param  [type] $id            [项目id ，如果存在则为修改项目]
 * @return [type]                [description]
 */
    public static function screate($CustomerId, $ProjectTypeId, $data)
    {
        $sb = CustomerProjectType::get($ProjectTypeId);
        if(empty($sb)){
            return moerr('项目分类错误');
        }
        if(empty($model)){
            $cdata = new CustomerProject;
            $cdata['UpTime']      = date('Y-m-d H:i:s', time());
            $cdata['CustomerId']  = $CustomerId;
        }else{
            $cdata = $model;
        }
        $cdata['ProjectTypeId']     = $ProjectTypeId;        
        $cdata['BussInessId'] = Pub::userid();
        $file = ['Name','KeyTime','Location','Parameter','State','Level','Note','Img'];
        foreach ($file as $key => $value) {
            if(!empty($data[$value])){
                $cdata->$value = $data[$value];
            }
        }
        $cdata->save($cdata);
        return sok('登记成功');
    }

    /**
     * 修改客户项目
     * @param  [type] $CustomerId    [description]
     * @param  [type] $ProjectTypeId [description]
     * @param  [type] $data          [description]
     * @param  [type] $id            [项目id ，如果存在则为修改项目]
     * @return [type]                [description]
     */
    public static function reinfo($model, $ProjectTypeId, $data)
    {
        $sb = CustomerProjectType::get($ProjectTypeId);
        if(empty($sb)){
            return moerr('项目分类错误');
        }
        $cdata = $model;
        $cdata['UpTime'] = date('Y-m-d H:i:s', time());
        $cdata['ProjectTypeId']     = $ProjectTypeId;        
        $cdata['BussInessId'] = Pub::userid();
        $file = ['Name','KeyTime','Location','Parameter','State','Level','Note','Img'];
        foreach ($file as $key => $value) {
            if(!empty($data[$value])){
                $cdata->$value = $data[$value];
            }
        }
        $cdata->save($cdata);
        return sok('修改成功');
    }


    /**
     * 删除项目信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function deleteinfo($model){
        $sb = $model;
        if(empty($sb)||$sb['IsDelete']==1){
            return moerr('没找到相关项目');
        }
        $sb->IsDelete = 1;
        $sb->save();
        return sok('刪除成功');
    }

}
